﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDAL;
using Model;

namespace DAL
{
    public class D_Template : ITemplate
    {
        WishDayEntities _e;
        public D_Template()
        {
            _e = new WishDayEntities();
        }
        public void Add(Template obj)
        {
            _e.Template.Add(obj);
            _e.SaveChanges();
        }

        public void Delete(int id)
        {
           var data= _e.Template.Find(id);
            _e.Template.Remove(data);
            _e.SaveChanges();
        }

        public Template FindById(int id)
        {
            return _e.Template.Find(id);
        }

        public List<Template> GetAll()
        {
            return _e.Template.ToList();
        }

        public Full_Template GetFullTemplate(int id)
        {
            var data = new Full_Template(){
                BasicInform=new D_BasicInformation().FindByTemplateID(id),
                JobIntention=new D_JobIntention().FindByTemplateID(id),
                ListExperience=new D_ExperienceDetail().GetDataByTemplate(id),
                ListCustom = new D_CustomDetails().GetDataByTemplate(id)
            };
            return data;
        }

        public List<Template> GetPrivateTemplates()
        {
            var data = from ud in _e.UserCloudData.AsEnumerable()
                       join tp in _e.Template.AsEnumerable() on ud.TemplateID equals tp.TemplateID
                       select new Template {
                           TemplateID=tp.TemplateID,
                           Tname=tp.Tname,
                           Describe=tp.Describe,
                           Timg=tp.Timg,
                           Style=tp.Style,
                           Price=tp.Price,
                           Thumbsup=tp.Thumbsup,
                           ClassifyID=tp.ClassifyID
                       };
            return data.ToList();
        }

        public List<Template> GetPublicTemplates()
        {
            var data = from tp in _e.Template.AsEnumerable()
                       from ud in _e.UserCloudData.AsEnumerable()
                       where tp.TemplateID != ud.TemplateID
                       select new Template
                       {
                           TemplateID = tp.TemplateID,
                           Tname = tp.Tname,
                           Describe = tp.Describe,
                           Timg = tp.Timg,
                           Style = tp.Style,
                           Price = tp.Price,
                           Thumbsup = tp.Thumbsup,
                           ClassifyID = tp.ClassifyID
                       };


            return data.ToList();
        }

        public List<Template> GetTemplatesByUser(int id)
        {
            var data = from tp in _e.Template.AsEnumerable()
                       join ud in _e.UserCloudData.Where(x => x.UserID == id) on tp.TemplateID equals ud.TemplateID
                       select new Template
                       {
                           TemplateID = tp.TemplateID,
                           Tname = tp.Tname,
                           Describe = tp.Describe,
                           Timg = tp.Timg,
                           Style = tp.Style,
                           Price = tp.Price,
                           Thumbsup = tp.Thumbsup,
                           ClassifyID = tp.ClassifyID
                       };

            return data.ToList();
        }

        public void Update(int id, Template obj)
        {
            var data = _e.Template.Find(id);
            data.Tname = obj.Tname;
            data.Describe = obj.Describe;
            data.Timg = obj.Timg;
            data.Style = obj.Style;
            data.Price = obj.Price;
            data.Thumbsup = obj.Thumbsup;
            data.ClassifyID = obj.ClassifyID;
            _e.SaveChanges();
        }
    }
}
